﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace HPIT.Web
{
    /// <summary>
    /// 数据库访问助手类
    /// </summary>
    public class DbHelper
    {
        //1.定义数据库连接字符串
        private static readonly string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

        /// <summary>
        /// 返回受影响行数（执行增删改）
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            //使用using执行完之后释放资源
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                //创建SqlCommand对象
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    //添加参数
                    if (parameters != null)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    //打开连接，先判断状态是否关闭
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    //执行操作
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// 返回首行首列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
        {
            //使用using执行完之后释放资源
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                //创建SqlCommand对象
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    //添加参数
                    if (parameters != null)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    //打开连接，先判断状态是否关闭
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    //执行操作
                    return cmd.ExecuteScalar();
                }
            }
        }

        /// <summary>
        /// 返回SqlDataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] parameters)
        {
            //使用using执行完之后释放资源
            SqlConnection conn = new SqlConnection(connStr);
            //创建SqlCommand对象
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                //添加参数
                if (parameters != null)
                {
                    cmd.Parameters.AddRange(parameters);
                }
                //打开连接，先判断状态是否关闭
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                //执行操作
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }

        }
    }
}